const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const { VueLoaderPlugin } = require('vue-loader')
const webpack = require('webpack')
module.exports = {
  entry: './src/main.js',
  output: {
    path: path.join(__dirname, './dist'),
    filename: 'bundle.[contenthash:6].js',
  },
  // devtool: 'cheap-module-source-map',
  devServer: {
    hot: true,
    port: '1024',
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        //use中放的就是加载器的名称，要注意，调用的顺序是从后向前的
        use: ['style-loader', 'css-loader'],
      },
      {
        //less文件
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader'],
      },
      {
        //sass文件
        test: /\.sass$/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
      },
      {
        //图片文件
        test: /\.(jpg|gif|png|jpeg|svg|bmp)$/,
        use: {
          loader: 'url-loader',
          options: {
            esModule: false,
            outputPath: 'assets',
            name: '[name].[contenthash:6].[ext]',
            publicPath: './assets',
          },
        },
      },
      {
        //字体文件
        test: /\.(woff|woff2|eot|ttf)$/,
        use: {
          loader: 'url-loader',
          options: {
            esModule: false,
            outputPath: 'assets/font',
            name: '[name].[contenthash:6].[ext]',
            publicPath: './assets/font',
          },
        },
      },
      {
        //es6
        test: /\.js$/,
        //排除node_modules中的内容
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.vue$/,
        use: ['vue-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'My Vue Webpack',
      template: './public/index.html',
      url: './public/', //需要这里传参
      inject: true,
      favicon: path.resolve('public/favicon.ico'),
    }),
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify(''),
    }),
  ],
}
